package com.szh.mysobproject.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.szh.mysobproject.model.msg.*
import com.szh.mysobproject.paging.source.msg.factory.*
import com.szh.mysobproject.paging.source.msg.impl.MsgPagingSourceImpl
import com.szh.mysobproject.viewmodel.app.Repository
import kotlinx.coroutines.flow.Flow

/**
 * @author : create by  szh
 * @date : 2022/7/11 14:57
 * @desc :消息viewModel
 */

class MsgViewModel : ViewModel() {

    /**
     * 获取未读消息
     */
    fun getUnReadMsgCount() = Repository.getUnReadMsgCount()

    /**
     * 获取点赞消息
     */
    fun getLikeMsgList(): Flow<PagingData<LikeMsg.Content>> {
        return Pager(config = PagingConfig(DEFAULT_PAGE_SIZE),
            pagingSourceFactory = {
                MsgPagingSourceImpl<LikeMsg.Content>(LikeMsgListFactory())
            }).flow.cachedIn(viewModelScope)
    }

    companion object {
        private const val DEFAULT_PAGE_SIZE = 30
    }

    /**
     * 获取摸鱼列表消息
     */
    fun getMomentMsgList(): Flow<PagingData<MomentMsg.Content>> {
        return Pager(config = PagingConfig(DEFAULT_PAGE_SIZE),
            pagingSourceFactory = {
                MsgPagingSourceImpl<MomentMsg.Content>(FishMsgListFactory())
            }).flow.cachedIn(viewModelScope)

    }

    /**
     * 已读摸鱼消息
     */
    fun readMomentMsg(msgId: String) = Repository.readMomentMsg(msgId)

    /**
     * 获取@我的消息
     */
    fun getAtMeMsgList(): Flow<PagingData<AtMeMsg.Content>> {
        return Pager(config = PagingConfig(30),
            pagingSourceFactory = {
                MsgPagingSourceImpl<AtMeMsg.Content>(AtMeMsgListFactory())
            }).flow.cachedIn(viewModelScope)
    }

    /**
     * 已读@我的消息
     */
    fun readAtMeMsg(msgId: String) = Repository.readAtMeMsg(msgId)

    /**
     * 获取问答消息列表
     */
    fun getQaMsgList(): Flow<PagingData<QaMsg.Content>> {
        return Pager(config = PagingConfig(30),
            pagingSourceFactory = {
                MsgPagingSourceImpl<QaMsg.Content>(QaMsgListFactory())
            }).flow.cachedIn(viewModelScope)
    }

    /**
     * 更新问答消息状态
     */
    fun readQaMsg(msgId: String) = Repository.readQaMsg(msgId)

    /**
     * 获取系统消息
     */
    fun getSystemMsgList(): Flow<PagingData<SystemMsg.Content>> {
        return Pager(config = PagingConfig(30),
            pagingSourceFactory = {
                MsgPagingSourceImpl<SystemMsg.Content>(SystemMsgListFactory())
            }).flow.cachedIn(viewModelScope)
    }

    /**
     * 一键已读全部
     */
    fun readAllMsg() = Repository.readAllMsg()

}